package com.uphone.magicleafteacher.view.calendar;

import android.support.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.view.MyBaseViewHolder;

import java.util.List;

public class CalendarAdapter extends BaseQuickAdapter<DayBean, MyBaseViewHolder> {
    private final int mTodayDay;
    private int mMonth;
    private int mYear;
    private final int mTodayMonth;
    private final int mTodayYear;
    private int mSelectPosition = -1;


    public CalendarAdapter(int year, int month, int day) {
        super(R.layout.rv_date_item);
        this.mYear = year;
        this.mMonth = month;
        this.mTodayYear = year;
        this.mTodayMonth = month;
        this.mTodayDay = day;
    }

    @Override
    protected void convert(MyBaseViewHolder helper, DayBean item) {
        if (mSelectPosition == -1 && item.getDay() == mTodayDay && item.getType() == 0) {
            mSelectPosition = helper.getLayoutPosition();
        }
        helper.setText(R.id.tv_day, item.getDay() + "");
        if (item.getType() != 0) {
            helper.setTextColor(R.id.tv_day, mContext.getResources().getColor(R.color.gray9))
                    .setBackgroundRes(R.id.tv_day, R.color.white);
        } else {
            if (mSelectPosition == helper.getLayoutPosition()) {
                // 选中
                helper.setTextColor(R.id.tv_day, mContext.getResources().getColor(R.color.white))
                        .setBackgroundRes(R.id.tv_day, R.drawable.shape_oval_bg_theme);
            } else if (mTodayDay == item.getDay() && mTodayMonth == mMonth && mTodayYear == mYear) {
                // 未选中 但是今天
                helper.setTextColor(R.id.tv_day, mContext.getResources().getColor(R.color.themecolor))
                        .setBackgroundRes(R.id.tv_day, R.color.white);
            } else {
                // 未选中
                helper.setTextColor(R.id.tv_day, mContext.getResources().getColor(R.color.gray2))
                        .setBackgroundRes(R.id.tv_day, R.color.white);
            }
        }
    }

    public void setSelectPosition(int selectPosition) {
        if (selectPosition == mSelectPosition || getData().get(selectPosition).getType() != 0) {
            return;
        }
        int tempPosition = this.mSelectPosition;
        this.mSelectPosition = selectPosition;
        notifyItemChanged(tempPosition);
        notifyItemChanged(mSelectPosition);
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    @Override
    public void setNewData(@Nullable List<DayBean> data) {
        if (mSelectPosition != -1 && (mSelectPosition > data.size() || data.get(mSelectPosition).getType() != 0)) {
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).getType() == 0) {
                    mSelectPosition = i;
                    break;
                }
            }
        }
        super.setNewData(data);
    }

    public void setYear2Month(int year, int month) {
        setYear(year);
        setMonth(month);
    }

    public String getMonthFormat() {
        return mYear + "年" + mMonth + "月";
    }

    public String getDateFormat() {
        return mYear + "年" + mMonth + "月" + 1;
    }

    public int getMonth() {
        return mMonth;
    }

    public int getYear() {
        return mYear;
    }

    public void setMonth(int month) {
        this.mMonth = month;
    }

    public void setYear(int year) {
        this.mYear = year;
    }

    public int getSelectDay() {
        return mSelectPosition == -1 ? mTodayDay : getData().get(mSelectPosition).getDay();
    }

    // 今天之后（包含今天）
    public boolean isTodayAfter() {
        return mYear > mTodayYear || (mYear == mTodayYear && (mMonth > mTodayMonth || (mMonth == mTodayMonth && getSelectDay() >= mTodayDay)));
    }
}
